<?php

namespace QnuiRings\Controller;
use Think\Controller;
// 软件名称：互动折扣王-连环折扣-倒计时折扣 
// 代码日期：v3.0 2017-5-10
// 作者：杨道泉 
// 文件名：QnuiZhekouAction.class.php 
// 功能： 使用千牛UI发布限时折扣

class QnuiRingsZhekouController extends QnuiBaseController {
	
	public function create_item_active(){
		$this->tagName=I("tagName");
		$this->startTime=date("Y-m-d H:i:s",I("startTime"));
		$this->endTime=date("Y-m-d H:i:s",I("endTime"));
		$this->notice=I("notice");
		$this->discountRate=I("discountRate")*100;
		$this->decreaseAmount=I("decreaseAmount")*100;
		$this->task_tag_id=I("task_tag_id");
		$this->task_tag_name=I("task_tag_name");
		
		//创建部分商品参与的折扣
		Vendor('taobaosdk/top/request/PromotionmiscCommonItemActivityAddRequest');
		$req = new \PromotionmiscCommonItemActivityAddRequest;
		$req->setName($this->tagName);
		$req->setDescription($this->notice);
		$req->setStartTime($this->startTime);
		$req->setEndTime($this->endTime);
		$req->setIsUserTag("true");
		$req->setUserTag("NEW_".$this->task_tag_id);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		
		$this->ajaxReturn($resp);
	}
	public function add_active_to_database(){
		// 2017-07-11
		// 杨道泉
		$task_id = I("task_id");
		$PROM = M("Promotions");
		$data=Array();
		$data["sid"]=session("SHOP_ID");
		$data["activity_id"]=I("activity_id");
		$data["name"]=I("tagName");
		$data["participate_range"]=I("itemArea")=="all"?1:0;
		$data["start_time"]=date("Y-m-d H:i:s",I("startTime"));
		$data["end_time"]=date("Y-m-d H:i:s",I("endTime"));
		$data["is_user_tag"]=true;
		$data["tag_name"]=I("task_tag_name");
		$data["tag_id"]=I("task_tag_id");
		$data["is_decrease_money"]=(I("decreaseAmount")*100?"true":"");
		$data["decrease_amount"]=I("decreaseAmount")*100;
		$data["is_discount"]=I("discountRate")*100?"true":"";
		$data["is_limit_buy"]=I("act_type")=="limit"?1:0;
		$data["status"]=1;
		
		$re = new \StdClass();
		$re->status = $PROM->add($data);
		
		$TAGTASK = M("TagTask");
		$TAGTASK->where(Array("id"=>$task_id,"sid"=>session("SHOP_ID")))->setInc('promotion_count');
		
		if(!$re->status){
			$re->code=910;
			$re->sub_msg="抱歉，保存出错。";
		}
		$this->ajaxReturn(pre_resp($re));
	}
	
	public function get_active_list(){
		// participate_range 1 全店折扣，0单品折扣
		$page = I("page");
		$page_size = I("page_size");
		$active_type = I("active_type");
		$tag_id = I("tag_id");

		switch($active_type){
			case "limit" :
			$where = Array(
						"sid"=>session("SHOP_ID"),
						"is_limit_buy"=>1,
						"tag_id"=>$tag_id
						);
			break;
			case "allshop" :
			$field = ",is_decrease_money,decrease_amount,is_discount,discount_rate";
			$where = Array(
						"sid"=>session("SHOP_ID"),
						"participate_range"=>1,
						"is_limit_buy"=>0,
						"tag_id"=>$tag_id
						);
			break;
			case "item" :
			$where = Array(
						"sid"=>session("SHOP_ID"),
						"participate_range"=>0,
						"is_limit_buy"=>0,
						"tag_id"=>$tag_id
						);
			break;
		}
		$PROM = M("Promotions");
		$total_count = $PROM->where($where)->count();	
		
		$resp = new \StdClass();
		$resp->total_count = $total_count;

		if($total_count>0){
			$proms = $PROM->where($where)
						  ->limit(($page-1),$page*$page_size)
						  ->getField("activity_id,description,is_user_tag,end_time,start_time,name".$field);

			$resp->activity_list->common_item_activity = array_values($proms);
		}
		
		$this->ajaxReturn(pre_resp($resp));
	}
	
	
	
	public function remove_item_active(){
		$activity_id = I("activity_id");
		Vendor('taobaosdk/top/request/PromotionmiscCommonItemActivityDeleteRequest');
		$req = new \PromotionmiscCommonItemActivityDeleteRequest;
		$req->setActivityId($activity_id);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		
		$this->ajaxReturn($resp);
	}
	public function remove_shop_active(){
		$activity_id = I("activity_id");
		Vendor('taobaosdk/top/request/PromotionmiscItemActivityDeleteRequest');
		$req = new \PromotionmiscItemActivityDeleteRequest;
		$req->setActivityId($activity_id);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		$this->ajaxReturn($resp);
	}
	//以下功能转移到QnuiZhekouLimitController
	// public function remove_limit_active(){
		// $activity_id = I("activity_id");
		// Vendor('taobaosdk/top/request/UmpActivityDeleteRequest');
		// $req = new \UmpActivityDeleteRequest;
		// $req->setActId($activity_id);
		// $resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		
		// $this->ajaxReturn($resp);
	// }
	
	public function remove_active_in_database(){
		$activity_id = I("activity_id");
		$tag_id = I("tag_id");
		$PROM = M("Promotions");
		$re = $PROM->where(Array("activity_id"=>$activity_id,"sid"=>session("SHOP_ID")))->delete();	
		
		
		$TAGTASK = M("TagTask");
		$TAGTASK->where(Array("tag_id"=>$tag_id,"sid"=>session("SHOP_ID")))->setDec('promotion_count');
		
		$resp = new \StdClass();
		if(!$re){
			$resp->code = 910;
			$resp->is_success = false;
			$re->sub_msg="抱歉，删除出错。您是不是已经删除过了？";
		}
		$this->ajaxReturn(pre_resp($resp));
	}
	public function get_items_in_active(){
		$activity_id = I("activity_id");		
		$resp = new \StdClass();
		$where = Array("activity_id"=>$activity_id,"sid"=>session("SHOP_ID"));
		$ITEMS = M("PromotionItems");
		$resp->total_count = $ITEMS->where($where)->count();	
		if($resp->total_count>0){
			$items = $ITEMS->where($where)
						  ->getField("num_iid,detail_id,title,pic_url,price,promotion_type,promotion_value,is_limit_buy,limit_buy_count,'done' AS status");
			foreach($items as $key=>$item){
				$items[$key]["value"] = $item["promotion_value"]/100;
				$items[$key]["ztype"] = ($item["promotion_type"]=="0"?"decreaseAmount":"discountRate");
				$items[$key]["finaPrice"] = ($item["promotion_type"]=="0"?($item["price"]-$item["promotion_value"]):($item["price"]*($item["promotion_value"]/10)))/100;
			}
			$resp->items = $items;
		}
		//{"detail_list":{"common_item_detail":{"activity_id":"3877340127","detail_id":"20926215613","item_id":"540024045149","promotion_type":"0","promotion_value":"100"}},"is_success":"true","total_count":"1","request_id":"10fcnzrxac4q9","code":"200"}
		$this->ajaxReturn(pre_resp($resp));
	}
	public function add_items_to_active(){
		$activity_id = I("activity_id");
		$item_id = I("item_id");
		$promotion_type = I("promotion_type");
		$promotion_value = I("promotion_value")*100;
		Vendor('taobaosdk/top/request/PromotionmiscCommonItemDetailAddRequest');
		$req = new \PromotionmiscCommonItemDetailAddRequest;
		$req->setActivityId($activity_id);
		$req->setItemId($item_id);
		$req->setPromotionType($promotion_type);
		$req->setPromotionValue($promotion_value);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		$this->ajaxReturn($resp);
	}
	
	public function save_items_to_database(){
		$data["activity_id"] = I("activity_id");
		$data["detail_id"] = I("detail_id");
		$data["promotion_type"] = I("promotion_type");
		$data["promotion_value"] = I("promotion_value")*100;
		$data["num_iid"] = I("num_iid");
		$data["title"] = I("title");
		$data["pic_url"] = I("pic_url");
		$data["price"] = I("price")*100;
		$data["is_limit_buy"] = I("act_type")=="limit"?1:0;
		$data["limit_buy_count"] = I("limit_buy_count");
		$data["sid"] = session("SHOP_ID");
		
		$ITEMS = M("PromotionItems");
		$re = $ITEMS->add($data);
		
		$resp = new \StdClass();
		if(!$re){
			$resp->code = 910;
			$resp->is_success = false;
			$resp->sub_msg="抱歉，添加出错。";
		}
		$this->ajaxReturn(pre_resp($resp));
	}
	
	
	public function create_limit_active(){
		$this->tagName=I("tagName");
		$this->startTime=date("Y-m-d H:i:s",I("startTime"));
		$this->endTime=date("Y-m-d H:i:s",I("endTime"));
		$this->notice=I("notice");
		$this->task_tag_id=I("task_tag_id");
		$this->task_tag_name=I("task_tag_name");

		$WEI = D("UmpWeiZheRing");
		$WEI->name = $this->tagName;
		$WEI->description = $this->notice;
		$WEI->tag_id = $this->task_tag_id;
		$WEI->start_time = $this->startTime;
		$WEI->end_time = $this->endTime;
		$WEI->ParticipateRange = true;
		$resp = pre_resp($WEI->CreateActivity());
		
		$this->ajaxReturn($resp);
	}
	public function add_limit_to_active(){
		$actId = I("activity_id");
		$promotion_type = I("promotion_type");
		$promotion_value = I("promotion_value");
		$price = I("price");
		$is_limit_buy = I("is_limit_buy");
		$limit_count = I("limit_count")?I("limit_count"):9999;
		$item_id = I("item_id");
		$tag_id = I("tag_id");
		
		if($promotion_type==1){//打折的变更为减钱
			$promotion_value=($price-$price*($promotion_value/10))*100;
		}else{
			$promotion_value=$promotion_value*100;
		}
		
		$itemSet["itemId"] = $item_id;
		$itemSet["tag_id"] = $tag_id;
		$itemSet["limitCount"] = $is_limit_buy?$limit_count:9999;
		$itemSet["countAt"] = 0;
		$itemSet["enableMultiple"] = false;
		$itemSet["amountAt"] = 0;
		$itemSet["priceMultiple"] = false;
		$itemSet["calcDecrMultiple"] = true;
		$itemSet["decrease"] = true;
		$itemSet["decreaseMoney"] = $promotion_value;
		$itemSet["isAddTag"] = false;
		$itemSet["addTagId"] = 0;
		$itemSet["isRemoveTag"] = false;
		$itemSet["removeTagId"] = 0;
		
		$WEI = D("UmpWeiZheRing");
		$WEI->actId = $actId;
		$WEI->itemSet = $itemSet;
		$resp = pre_resp($WEI->CreateItemDetail());
		$resp->params = $itemSet;
		$this->ajaxReturn($resp);
	}
	
	public function edit_limit_in_active(){
		$detailId = I("detail_id");
		$actId = I("activity_id");
		$promotion_type = I("promotion_type");
		$promotion_value = I("promotion_value");
		$price = I("price");
		$is_limit_buy = I("is_limit_buy");
		$limit_count = I("limit_count");
		$item_id = I("item_id");
		$tag_id = I("tag_id");
		
		if($promotion_type==1){//打折的变更为减钱
			$promotion_value=($price-$price*($promotion_value/10))*100;
		}else{
			$promotion_value=$promotion_value*100;
		}
		
		$itemSet["itemId"] = $item_id;
		$itemSet["tag_id"] = $tag_id;
		$itemSet["limitCount"] = $is_limit_buy?$limit_count:9999;
		$itemSet["countAt"] = 0;
		$itemSet["enableMultiple"] = false;
		$itemSet["amountAt"] = 0;
		$itemSet["priceMultiple"] = false;
		$itemSet["calcDecrMultiple"] = true;
		$itemSet["decrease"] = true;
		$itemSet["decreaseMoney"] = $promotion_value;
		$itemSet["isAddTag"] = false;
		$itemSet["addTagId"] = 0;
		$itemSet["isRemoveTag"] = false;
		$itemSet["removeTagId"] = 0;
		
		$WEI = D("UmpWeiZheRing");
		$WEI->detailId = $detailId;
		$WEI->itemSet = $itemSet;
		$resp = pre_resp($WEI->editItemDetail());
		$resp->params = $itemSet;
		$this->ajaxReturn($resp);
	}
	
	public function add_limit_postfee_to_active(){
		$actId = I("activity_id");
		$promotion_type = I("promotion_type");//一定是1，只能打折
		$promotion_value = I("promotion_value")*100;
		$price = I("price");
		$is_limit_buy = I("is_limit_buy");
		$limit_count = I("limit_count");
		$item_id = I("item_id");
		$tag_id = I("tag_id");
		
		$itemSet["itemId"] = $item_id;
		$itemSet["limitCount"] = $is_limit_buy?$limit_count:9999;
		$itemSet["discount"] = true;
		$itemSet["discountRate"] = $promotion_value;
		$itemSet["deltag"] = true;
		$itemSet["tagId"] = $tag_id;
		$itemSet["freepost"] = false;
		
		$WEI = D("UmpWeiZheRing");
		$WEI->actId = $actId;
		$WEI->itemSet = $itemSet;
		$resp = pre_resp($WEI->CreateItemDetail());
		$resp->params = $itemSet;
		$this->ajaxReturn($resp);
	}
	
	public function get_limit_in_active(){
		$activity_id = I("activity_id");
		$page = I("page");
		$page_size = I("page_size");
		Vendor('taobaosdk/top/request/UmpDetailsGetRequest');
		$req = new \UmpDetailsGetRequest;
		$req->setActId($activity_id);
		$req->setPageNo($page);
		$req->setPageSize($page_size);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		$this->ajaxReturn($resp);
	}
	public function edit_limit_postfee_in_active(){
		$detailId = I("detail_id");
		$promotion_type = I("promotion_type"); //这个type一定是1，使用的工具只能打折
		$promotion_value = I("promotion_value")*100;
		$price = I("price");
		$is_limit_buy = I("is_limit_buy");
		$limit_count = I("limit_count");
		$item_id = I("item_id");
		$tag_id = I("tag_id");
		
		$itemSet["itemId"] = $item_id;
		$itemSet["limitCount"] = $is_limit_buy?$limit_count:9999;
		$itemSet["discount"] = true;
		$itemSet["discountRate"] = $promotion_value;
		$itemSet["deltag"] = true;
		$itemSet["tagId"] = $tag_id;
		$itemSet["freepost"] = false;
		
		$WEI = D("UmpWeiZheRing");
		$WEI->detailId = $detailId;
		$WEI->itemSet = $itemSet;
		$resp = pre_resp($WEI->editItemDetail());
		$resp->params = $itemSet;
		$this->ajaxReturn($resp);
	}
	public function edit_item_in_active(){
		$activity_id = I("activity_id");
		$detail_id = I("detail_id");
		$item_id = I("item_id");
		$promotion_type = I("promotion_type");
		$promotion_value = I("promotion_value")*100;
		Vendor('taobaosdk/top/request/PromotionmiscCommonItemDetailUpdateRequest');
		$req = new \PromotionmiscCommonItemDetailUpdateRequest;
		$req->setDetailId($detail_id);
		$req->setActivityId($activity_id);
		$req->setItemId($item_id);
		$req->setPromotionType($promotion_type);
		$req->setPromotionValue($promotion_value);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		$this->ajaxReturn($resp);
	}
	public function update_items_in_database(){
		$detail_id = I("detail_id");
		$data["promotion_type"] = I("promotion_type");
		$data["promotion_value"] = I("promotion_value")*100;
		$data["is_limit_buy"] = I("is_limit_buy")?1:0;
		$data["limit_buy_count"] = I("limit_count");
		
		
		$ITEMS = M("PromotionItems");
		$re = $ITEMS->where(Array("detail_id"=>$detail_id,"sid"=>session("SHOP_ID")))->save($data);
		
		$resp = new \StdClass();
		if($re===false){
			$resp->code = 910;
			$resp->is_success = false;
			$re->sub_msg="抱歉，添加出错。";
		}
		$this->ajaxReturn(pre_resp($resp));
	}
	
	
	public function remove_items_in_active(){
		$activity_id = I("activity_id");
		$detail_id = I("detail_id");
		Vendor('taobaosdk/top/request/PromotionmiscCommonItemDetailDeleteRequest');
		$req = new \PromotionmiscCommonItemDetailDeleteRequest;
		$req->setActivityId($activity_id);
		$req->setDetailId($detail_id);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		
		$this->ajaxReturn($resp);
	}
	
	//以下功能转移到QnuiZhekouLimitController
	// public function remove_limit_in_active(){
		// $detail_id = I("detail_id");
		// Vendor('taobaosdk/top/request/UmpDetailDeleteRequest');
		// $req = new \UmpDetailDeleteRequest;
		// $req->setDetailId($detail_id);
		// $resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		// $this->ajaxReturn($resp);
	// }
	
	public function remove_items_in_database(){
		$activity_id = I("activity_id");
		$detail_id = I("detail_id");
		
		$ITEMS = M("PromotionItems");
		$re = $ITEMS->where(Array("activity_id"=>$activity_id,"detail_id"=>$detail_id,"sid"=>session("SHOP_ID")))->delete();
		
		$resp = new \StdClass();
		if(!$re){
			$resp->code = 910;
			$resp->is_success = false;
			$re->sub_msg="抱歉，删除出错。";
		}
		$this->ajaxReturn(pre_resp($resp));
	}
	
	public function edit_item_active(){
		$activity_id = I("activity_id");
		$name = I("name");
		$description = I("description");
		$start_time = date("Y-m-d H:i:s",strtotime(I("start_time")));;
		$end_time = date("Y-m-d H:i:s",strtotime(I("end_time")));;
		$is_user_tag = I("is_user_tag");
		$tag_id = I("tag_id");
		Vendor('taobaosdk/top/request/PromotionmiscCommonItemActivityUpdateRequest');
		$req = new \PromotionmiscCommonItemActivityUpdateRequest;
		$req->setActivityId($activity_id);
		$req->setName($name);
		$req->setDescription($description);
		$req->setStartTime($start_time);
		$req->setEndTime($end_time);
		$req->setIsUserTag($is_user_tag?true:false);
		$req->setUserTag("NEW_".$tag_id);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		
		$this->ajaxReturn($resp);
	}
	
	public function edit_limit_active(){
		$activity_id=I("activity_id");
		$tagName=I("name");
		$startTime=date("Y-m-d H:i:s",strtotime(I("start_time")));
		$endTime=date("Y-m-d H:i:s",strtotime(I("end_time")));
		$notice=I("description");
		$tag_id=I("tag_id");

		$WEI = D("UmpWeiZheRing");
		$WEI->name = $tagName;
		$WEI->description = $notice;
		$WEI->tag_id = $tag_id;
		$WEI->start_time = $startTime;
		$WEI->end_time = $endTime;
		$WEI->actId = $activity_id;
		$resp = pre_resp($WEI->editActivity());
		
		$this->ajaxReturn($resp);
	}
	
	public function save_active_in_database(){
		$activity_id = I("activity_id");
		$PROM = M("Promotions");
		$data=Array();
		$data["name"]=I("name");
		$data["description"]=I("description");
		$data["start_time"]=date("Y-m-d H:i:s",strtotime(I("start_time")));
		$data["end_time"]=date("Y-m-d H:i:s",strtotime(I("end_time")));
		$act_type=I("act_type");
		if($act_type=="limit"||$act_type="item"){
			$data["is_decrease_money"]=(I("decreaseAmount")*100?"true":"");
			$data["decrease_amount"]=I("decreaseAmount")*100;
			$data["is_discount"]=I("discountRate")*100?"true":"";
			$data["is_limit_buy"]=I("act_type")=="limit"?1:0;
		}
		$re = new \StdClass();
		$where = Array("sid"=>session("SHOP_ID"),"activity_id"=>$activity_id);
		$re->status = $PROM->where($where)->save($data);
		
		if($re->status===false){
			$re->code=910;
			$re->sub_msg="抱歉，保存出错。";
		}
		$this->ajaxReturn(pre_resp($re));
	}
	
	public function get_shop_active_list(){
		$page = I("page");
		$page_size = I("page_size");
		Vendor('taobaosdk/top/request/PromotionmiscItemActivityListGetRequest');
		$req = new \PromotionmiscItemActivityListGetRequest;
		$req->setPageNo($page);
		$req->setPageSize($page_size);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		
		$this->ajaxReturn($resp);
	}
	public function get_shop_active_detail(){
		$activity_id = I("activity_id");
		Vendor('taobaosdk/top/request/PromotionmiscItemActivityGetRequest');
		$req = new \PromotionmiscItemActivityGetRequest;
		$req->setActivityId($activity_id);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		
		$this->ajaxReturn($resp);
	}
	public function api(){
		Vendor('taobaosdk/top/request/UmpMbbsGetRequest');
		$req = new \UmpMbbsGetRequest;
		// $req->setType("resource");
		$resp = $this->c->execute($req);
		var_dump($resp);
	}
}